﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;
using YJKCodeBase.WinForm;
using YRevitJig;

namespace Balcony
{
	internal class Polyline
	{
		public Polyline(WindowHandle _hWnd, UIApplication app)
		{
			this._hWndRevit = _hWnd;
			this.m_app = app;
			this.m_doc = app.ActiveUIDocument.Document;
			this.m_uiDoc = app.ActiveUIDocument;
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public void GetCurves(ref List<Curve> lstCurves)
		{
			lstCurves.Clear();
			foreach (Curve item in this.m_listCurves)
			{
				lstCurves.Add(item);
			}
		}

		public void GetCurves(ref List<ModelCurve> lstCurves)
		{
			lstCurves.Clear();
			foreach (ModelCurve item in this.m_listModelCurves)
			{
				lstCurves.Add(item);
			}
		}

		public Result DrawPolylines(string message)
		{
			this.m_listCurves = new List<Curve>();
			this.m_listModelCurves = new List<ModelCurve>();
			Result result = 0;
			PolylineForm polylineForm = new PolylineForm(this._hWndRevit);
			polylineForm.Show(this._hWndRevit);
			int wMsg = 7;
			Polyline.SendMessage(this._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			try
			{
				for (;;)
				{
					if (this.m_listCurves.Count == 0 && !this.PickPoint(message + " 请选择起点", ref this.m_ptLast))
					{
						if (!polylineForm.PickButton)
						{
							break;
						}
						if (polylineForm.CurrentOperateType == PolylineForm.OperateType.OT_Delete)
						{
							polylineForm.SetDrawLineType();
						}
						polylineForm.PickButton = false;
					}
					else
					{
						switch (polylineForm.CurrentOperateType)
						{
						case PolylineForm.OperateType.OT_DrawLine:
							result = this.DrawLine();
							break;
						case PolylineForm.OperateType.OT_DrawArc3Pt:
							result = this.DrawArc3Pt();
							break;
						case PolylineForm.OperateType.OT_DrawArcCenter:
							result = this.DrawArcCenter();
							break;
						case PolylineForm.OperateType.OT_Delete:
							result = this.DeleteLast();
							polylineForm.SetDrawLineType();
							if (this.m_listCurves.Count == 0)
							{
								continue;
							}
							break;
						}
						if (result == Autodesk.Revit.UI.Result.Cancelled)
						{
							if (!polylineForm.PickButton)
							{
								goto IL_115;
							}
							polylineForm.PickButton = false;
						}
						else if (result == Autodesk.Revit.UI.Result.Failed)
						{
							goto Block_9;
						}
					}
				}
				polylineForm.Close();
				return Autodesk.Revit.UI.Result.Failed;
				IL_115:
				polylineForm.Close();
				goto IL_12A;
				Block_9:
				polylineForm.Close();
				IL_12A:
				polylineForm.Close();
			}
			catch (Exception ex)
			{
				polylineForm.Close();
				message = ex.Message;
				return Autodesk.Revit.UI.Result.Failed;
			}
			polylineForm.Close();
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private XYZ Get2DXYZ(XYZ point3d)
		{
			return new XYZ(point3d.X, point3d.Y, 0.0);
		}

		private bool PickPoint(string statusPrompt, ref XYZ ptPick)
		{
			try
			{
				ptPick = this.m_app.ActiveUIDocument.Selection.PickPoint(statusPrompt);
			}
			catch (Exception)
			{
				return false;
			}
			return true;
		}

		public Result DrawLine()
		{
			XLineJig xlineJig = new XLineJig(this.m_uiDoc);
			xlineJig.StartPoint = this.m_ptLast;
			XYZ xyz = xlineJig.PickEndPoint(this.m_ptLast, "请输入直线终点");
			xlineJig.Dispose();
			if (xyz == null)
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			xyz = new XYZ(xyz.X, xyz.Y, this.m_ptLast.Z);
			Line line = YJKLineEx.YJKGetBound(this.m_ptLast, xyz);
			this.m_ptLast = xyz;
			Transaction transaction = new Transaction(this.m_doc, "Polyline");
			transaction.Start();
			Plane plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(XYZ.BasisZ, xyz);
			SketchPlane sketchPlane = SketchPlane.Create(this.m_doc, plane);
			this.m_listModelCurves.Add(this.m_doc.Create.NewModelCurve(line, sketchPlane));
			this.m_listCurves.Add(line);
			transaction.Commit();
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public Result DrawArc3Pt()
		{
			XArc3PtJig xarc3PtJig = new XArc3PtJig(this.m_uiDoc);
			xarc3PtJig.StartPoint = this.m_ptLast;
			XYZ xyz = xarc3PtJig.PickEndPoint(this.m_ptLast, "请选择弧线终点");
			if (xyz == null)
			{
				xarc3PtJig.Dispose();
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			xyz = new XYZ(xyz.X, xyz.Y, this.m_ptLast.Z);
			XYZ xyz2 = xarc3PtJig.PickOnCurvePoint(this.m_ptLast, xyz, "请选择弧线中间任一点");
			xarc3PtJig.Dispose();
			if (xyz2 == null)
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			xyz2 = new XYZ(xyz2.X, xyz2.Y, this.m_ptLast.Z);
			Arc arc = Arc.Create(this.m_ptLast, xyz, xyz2);
			this.m_ptLast = xyz;
			Transaction transaction = new Transaction(this.m_doc, "Polyline");
			transaction.Start();
			Plane plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(XYZ.BasisZ, xyz);
			SketchPlane sketchPlane = SketchPlane.Create(this.m_doc, plane);
			this.m_listModelCurves.Add(this.m_doc.Create.NewModelCurve(arc, sketchPlane));
			this.m_listCurves.Add(arc);
			transaction.Commit();
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public Result DrawArcCenter()
		{
			XArcJig xarcJig = new XArcJig(this.m_uiDoc);
			XYZ xyz = xarcJig.PickCenterPoint("请选择弧线圆心");
			if (xyz == null)
			{
				xarcJig.Dispose();
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			XYZ xyz2 = xarcJig.PickEndPoint(xyz, this.m_ptLast, "请选择弧线终点");
			if (xyz2 == null)
			{
				xarcJig.Dispose();
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			xyz2 = new XYZ(xyz2.X, xyz2.Y, this.m_ptLast.Z);
			List<Curve> list = xarcJig.CreateCurves();
			xarcJig.Dispose();
			if (list.Count != 1)
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			Arc arc = list[0] as Arc;
			this.m_ptLast = xyz2;
			Transaction transaction = new Transaction(this.m_doc, "Polyline");
			transaction.Start();
			Plane plane = RevitVersionFuncs.CreatePlanByNormalAndOrigin(XYZ.BasisZ, xyz);
			SketchPlane sketchPlane = SketchPlane.Create(this.m_doc, plane);
			this.m_listModelCurves.Add(this.m_doc.Create.NewModelCurve(arc, sketchPlane));
			this.m_listCurves.Add(arc);
			transaction.Commit();
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public Result DeleteLast()
		{
			int count = this.m_listModelCurves.Count;
			if (count == 0)
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			Transaction transaction = new Transaction(this.m_doc);
			transaction.Start("tranDel");
			if (count > 1)
			{
				this.m_ptLast = this.m_listCurves[count - 2].GetEndPoint(1);
			}
			this.m_doc.DeleteElement(this.m_listModelCurves.ElementAt(count - 1));
			this.m_listModelCurves.RemoveAt(count - 1);
			this.m_listCurves.RemoveAt(count - 1);
			transaction.Commit();
			return Autodesk.Revit.UI.Result.Cancelled;
		}

		public static double Angle(XYZ vt)
		{
			return XYZ.BasisX.AngleOnPlaneTo(vt, XYZ.BasisZ);
		}

		public WindowHandle _hWndRevit;

		public List<Curve> m_listCurves;

		public List<ModelCurve> m_listModelCurves;

		private Document m_doc;

		private UIApplication m_app;

		private XYZ m_ptLast;

		private UIDocument m_uiDoc;
	}
}
